package com.indexServer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

public class httpServerByNetty {
	
	//设置根目录
	private static final String DEFAULT_URL="/src/com/";
	private static final String ROOT_URL="/home/han/Documents/HTML-Index";
	
	public static void main(String[] args ) 
	{
		
		 EventLoopGroup bossGroup=new NioEventLoopGroup();
	     EventLoopGroup workergGroup=new NioEventLoopGroup();
	     try 
	     {
	    	 //启动服务要初始化的所有东西
	    	 
	    	 ServerBootstrap b=new ServerBootstrap();
	    	 b.group(bossGroup, workergGroup).channel(NioServerSocketChannel.class).
	    	 			childHandler(new ChannelInitializer<SocketChannel>()
	    			 {
						@Override
						protected void initChannel(SocketChannel ch) throws Exception
						{
							// TODO Auto-generated method stub
							ch.pipeline().addLast(new HttpRequestDecoder(),
									new HttpObjectAggregator(65536),
									new HttpResponseEncoder(),
									new ChunkedWriteHandler(),
									new HttpServerHanderByNetty(ROOT_URL));
						} 
	    			 });
	    	ChannelFuture f=b.bind("127.0.0.1",8088).sync(); 
	    	f.channel().closeFuture().sync();
	     }
	     catch(Exception e)
	     {e.printStackTrace();}
	     finally
	     {
	    	 bossGroup.shutdownGracefully();
	    	 workergGroup.shutdownGracefully();
	     }
	}
}
